package com.bsoft.exchange.util;

import com.bsoft.exchange.domain.InspectionListDomain;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Dom4jUtil {
    public static String xmlToStr(String xmlStr) {
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xmlStr);
            Element rootElt = doc.getRootElement();
            Iterator iterator = rootElt.elementIterator();
            Element next = (Element) iterator.next();
            Iterator iterator1 = next.elementIterator();
            next = (Element) iterator1.next();
            Iterator iterator2 = next.elementIterator();
            next = (Element) iterator2.next();
            List elements = next.elements();
            if(elements.size() < 1){
                return next.getTextTrim();
            }
            Element e = (Element) elements.get(0);
            return e.asXML();

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<String> getResult(String xml, String nodeName) {
        Document doc = null;
        List<String> result = new ArrayList<>();
        try {
            doc = DocumentHelper.parseText(xml);
            Element rootElement = doc.getRootElement();
            Iterator iterator = rootElement.elementIterator(nodeName);
            while (iterator.hasNext()) {
                Element e = (Element) iterator.next();
                result.add(e.asXML());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getNodeValue(String xml, String nodeName){
        Document doc = null;
        String result = null;
        try {
            doc = DocumentHelper.parseText(xml);
            Element rootElement = doc.getRootElement();
            Element element = rootElement.element(nodeName);
            result = element.getTextTrim();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) throws DocumentException {
        String xml = "<![CDATA[<BSXml><MsgHeader><Sender>HIP</Sender><MsgType>CDR_5451</MsgType><MsgVersion>3.0</MsgVersion><Status>true</Status><ErrCode></ErrCode><Detail></Detail><ReturnCount>1</ReturnCount></MsgHeader><LabReportList _g=\"1\"><PatientType>01</PatientType><AuthorOrganization>46821120-9</AuthorOrganization><LabOrdersItemName>组织液生化</LabOrdersItemName><DCId>c744512827155a57</DCId><ReportDateTime>20200803T120800</ReportDateTime><Sex>1</Sex><PrintFlag>1</PrintFlag><VisitId>53b8546a9843ed5a</VisitId><LabOrderItemName></LabOrderItemName><Name>平台测试</Name><MpiId>4f51b3b93e01426d89e0bdd781942dff</MpiId><SampleNo>2</SampleNo><ReportTime></ReportTime><HospizationId>2007191001</HospizationId><ReportReviewTime>20200803T080854</ReportReviewTime><BarcodeNo>3008037846</BarcodeNo><PatientId>48ef7f151564a35c2210f6dd787693e8</PatientId><ClinicId>2007191001</ClinicId><DeptName>南区方便门诊</DeptName><Age>30岁</Age><ReportType>1</ReportType><SourceVisitId>695678</SourceVisitId></LabReportList></BSXml>]]>";
        Pattern pattern = Pattern.compile(".*<!\\[CDATA\\[(.*)\\]\\]>.*");
        Matcher matcher = pattern.matcher(xml);

        if (matcher.matches()) {
            System.out.println(matcher.group(1));
            List<String> labReportList = getResult(matcher.group(1), "LabReportList");
            System.out.println(labReportList);
            for (String str : labReportList) {
                InspectionListDomain inspectionListDomain = JaxbUtil.xmlToBean(str, InspectionListDomain.class);
                System.out.println(inspectionListDomain);
            }

        }


    }
}
